package com.example.zg.androidnewview;

import android.app.Dialog;
import android.app.DialogFragment;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.zg.androidnewview.customView.MBottomSheetDialogFragment;

public class BehaviorActivity extends AppCompatActivity {
    private static final String TAG = "BehaviorActivity";
    private BottomSheetBehavior mBottomSheetBehavior;
    private BottomSheetDialog bottomSheetDialog;
    private BottomSheetDialogFragment bottomSheetDialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_behavior);

        init();

    }

    private void init(){
        mBottomSheetBehavior=BottomSheetBehavior.from(findViewById(R.id.tab_layout));
        Button btn=findViewById(R.id.btn_bottom_sheet_control);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: state="+mBottomSheetBehavior.getState());
                if(mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_EXPANDED){
                    Log.i(TAG, "onClick: 折叠");
                    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }else if(mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_COLLAPSED){
                    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    Log.i(TAG, "onClick: 展开");
                }
            }
        });

        Button btn2=findViewById(R.id.btn_bottom_sheet_dialog);
        initDialog();
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bottomSheetDialog.isShowing()){
                    bottomSheetDialog.dismiss();
                }else{
                    bottomSheetDialog.show();
                }

            }
        });

        Button btn3=findViewById(R.id.btn_bottom_sheet_dialog_fragment);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bottomSheetDialogFragment==null){
                    bottomSheetDialogFragment=new MBottomSheetDialogFragment();
                }
                bottomSheetDialogFragment.show(getSupportFragmentManager(),MBottomSheetDialogFragment.class.getSimpleName());

            }
        });


    }

    private void initDialog(){
        if(bottomSheetDialog==null){
            bottomSheetDialog=new BottomSheetDialog(this);
            View view = getLayoutInflater().inflate(R.layout.dialog_item, null, false);
            bottomSheetDialog.setContentView(view);

        }
    }


}
